﻿using TinaX.XComponent;


public class MonoUI2Lua : XBehaviour
{
	XLua.LuaTable _luaIns;
	public override void OnEnable()
	{
		CallLuaFunction("_OnEnable");
	}

	public override void OnDisable()
	{
		CallLuaFunction("_OnDisable");
	}

	public override void Awake()
	{
		var luaClass = LuaMgr.luaEnv.Global.Get<XLua.LuaTable>(this.OnGetUIName());
		if (luaClass == null)
		{
			UnityEngine.Debug.LogError("不影响游戏运行：Can not find Lua Golbal Class:" + this.OnGetUIName());
			return;
		}
		var newFunc = luaClass.Get<XLua.LuaFunction>("New");
		_luaIns = newFunc.Func<MonoUI2Lua, XLua.LuaTable>(this);
		_luaIns.Set("gameObject", this.gameObject);
		_luaIns.Set("xComponent", this.xComponent);
		CallLuaFunction("Awake");
	}

	public override void Start()
	{
		CallLuaFunction("Start");
	}

	public override void OnDestroy()
	{
		if(CallLuaFunction("OnDestroy"))
			_luaIns.Dispose();
	}

	public override void Update()
	{

	}

	private string OnGetUIName()
	{
		return this.gameObject.name;
	}

	private bool CallLuaFunction(string funcName)
    {
		if (_luaIns == null)
			return false;
		_luaIns.Get<XLua.LuaFunction>(funcName).Call(_luaIns);
		return true;
	}
}

